package com.lyw.i09assistClass;

import java.util.concurrent.CountDownLatch;

/**
 * 计数器：可以通过构造方法初始一个值，使用 countDown() 方法让初始值每次都减一
 *        其中有一个 await() 阻塞方法，等待值变为 0 的时候继续处理业务
 *
 * @author lyw
 * @Date 2023/7/10-下午4:24
 */
public class CountDownLatchDemo {

  public static void main(String[] args) throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(6);
    for (int i = 1; i <= countDownLatch.getCount(); i++) {
      new Thread(() -> {
        System.out.println(Thread.currentThread().getName() + " 号同学离开了教室..");
        countDownLatch.countDown();
      }, String.valueOf(i)).start();
    }

    countDownLatch.await();
    System.out.println("班长关门了");
  }
}
